package com.youkeyi.ddy.cloud.application.system.server.controller;

import com.youkeyi.ddy.cloud.common.api.application.system.server.SystemServerCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.server.dto.SystemServerListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.server.dto.SystemServerListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.server.param.SystemServerCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.server.param.SystemServerDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.server.param.SystemServerListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.server.param.SystemServerUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.server.PublicServerCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.server.dto.PublicServerListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.server.param.PublicServerCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.server.param.PublicServerDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.server.param.PublicServerQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.server.param.PublicServerUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * SystemServerController
 *
 * @Serveror liuxiawang
 * @Date 2024/2/27 5:32 PM
 * @Location shenzhen.china
 */
@Service
public class SystemServerController implements SystemServerCloudService {
    
    @Autowired
    private PublicServerCloudService publicServerCloudService;

    @Override
    public SystemServerListCloudDto list(SystemServerListCloudParam param) {
        SystemServerListCloudDto dto = new SystemServerListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicServerQueryCloudParam serverQueryCloudParam = new PublicServerQueryCloudParam();
        serverQueryCloudParam.setServerPrimaryKeys(param.getServerPrimaryKeys());
        serverQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        serverQueryCloudParam.setCodes(param.getCodes());
        serverQueryCloudParam.setCodeFuzzy(param.getCodeFuzzy());
        serverQueryCloudParam.setNames(param.getNames());
        serverQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        serverQueryCloudParam.setLevel(param.getLevel());
        serverQueryCloudParam.setParentKeys(param.getParentKeys());
        serverQueryCloudParam.setPageSize(param.getPageSize());
        serverQueryCloudParam.setCurrentPage(param.getCurrentPage());
        serverQueryCloudParam.setNeedParent(Boolean.TRUE);
        PublicServerListCloudDto serverDtos = publicServerCloudService.query(serverQueryCloudParam);
        if (null != serverDtos && CollectionUtils.isNotEmpty(serverDtos.getRows())) {
            List<SystemServerListItemCloudDto> items = new ArrayList<>();
            serverDtos.getRows().forEach(server -> {
                SystemServerListItemCloudDto item = new SystemServerListItemCloudDto();
                item.setServerPrimaryKey(server.getServerPrimaryKey());
                item.setName(server.getName());
                item.setCode(server.getCode());
                item.setPrice(server.getPrice());
                item.setLevel(server.getLevel());
                item.setParentKey(server.getParentKey());
                item.setParentName(server.getParentName());
                item.setDesc(server.getDesc());
                item.setRemark(server.getRemark());
                item.setIsDeleted(server.getIsDeleted());
                item.setIsDeletedName(server.getIsDeletedName());
                item.setCreateUserKey(server.getCreateUserKey());
                item.setCreateTime(server.getCreateTime());
                item.setUpdateUserKey(server.getUpdateUserKey());
                item.setUpdateTime(server.getUpdateTime());
                item.setCreateUserName(server.getCreateUserName());
                item.setUpdateUserName(server.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(serverDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(SystemServerCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicServerCreateCloudParam serverCreateCloudParam = new PublicServerCreateCloudParam();
        serverCreateCloudParam.setCorpKey(corpKey);
        serverCreateCloudParam.setName(param.getName());
        serverCreateCloudParam.setCode(param.getCode());
        serverCreateCloudParam.setPrice(param.getPrice());
        serverCreateCloudParam.setParentKey(param.getParentKey());
        serverCreateCloudParam.setDesc(param.getDesc());
        serverCreateCloudParam.setRemark(param.getRemark());
        serverCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        serverCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return publicServerCloudService.create(serverCreateCloudParam);
    }

    @Override
    public ResponseContent update(SystemServerUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getServerPrimaryKey())
                || StringUtils.isBlank(param.getName())
                || StringUtils.isBlank(param.getCode())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicServerUpdateCloudParam serverUpdateCloudParam = new PublicServerUpdateCloudParam();
        serverUpdateCloudParam.setServerPrimaryKey(param.getServerPrimaryKey());
        serverUpdateCloudParam.setName(param.getName());
        serverUpdateCloudParam.setCode(param.getCode());
        serverUpdateCloudParam.setPrice(param.getPrice());
        serverUpdateCloudParam.setParentKey(param.getParentKey());
        serverUpdateCloudParam.setDesc(param.getDesc());
        serverUpdateCloudParam.setRemark(param.getRemark());
        serverUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        serverUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicServerCloudService.update(serverUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(SystemServerDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getServerPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicServerDeleteCloudParam serverDeleteCloudParam = new PublicServerDeleteCloudParam();
        serverDeleteCloudParam.setServerPrimaryKeys(param.getServerPrimaryKeys());
        serverDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        serverDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicServerCloudService.delete(serverDeleteCloudParam);
    }
}
